﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using School.Entities;
using System.Collections;
using System.IO;

namespace School.Admission
{
    public class StudentRelatives
    {
        private static SchoolEntities db = new SchoolEntities();

        public static Boolean Add(TE_Student_Relatives studentrelatives )
        {
            try
            {
                db.AddToTE_Student_Relatives(studentrelatives);
                db.SaveChanges();

                return true;
            }

            catch
            {
                return false;

            }
        }

        public static TE_Student_Relatives Single(long id)
        {
            return db.TE_Student_Relatives.Single(x => x.Id == id);
        }


        public static IEnumerable<TE_Student_Relatives> SingleByStudentId(long sid)
        {
            try
            {
                return db.TE_Student_Relatives.Where(x => x.Student_Id == sid);
            }
            catch
            {
                return null;
            }
        }



        public static IEnumerable<TE_Student_Relatives> All(Hashtable items)
        {
            SchoolEntities db = new SchoolEntities();

            var query = from studentrelatives in db.TE_Student_Relatives select studentrelatives;

            foreach (DictionaryEntry item in items)
            {
                if (item.Key == "Student_Id")
                {
                    query = query.Where(c => c.Student_Id  == long.Parse( item.Value.ToString()));
                }

                if (item.Key == "Details")
                {
                    query = query.Where(c => c.Details == item.Value);
                }

               
            }
            return query.ToList();
        }

        public static void Update(TE_Student_Relatives studentrelatives)
        {
            TE_Student_Relatives c = db.TE_Student_Relatives.Single(x => x.Id == studentrelatives.Id);


            c.Id = studentrelatives.Id;
            c.Student_Id = studentrelatives.Student_Id;
            c.Relative_student_Id = studentrelatives.Relative_student_Id;
            c.Details = studentrelatives.Details;
            db.SaveChanges();

        }

        public static Boolean Delete(long id)
        {
            try
            {
                db.TE_Student_Relatives.DeleteObject(db.TE_Student_Relatives.Single(x => x.Id == id));
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static Boolean DeletebyStudentId(long sid)
        {
            try
            {
                var data = db.TE_Student_Relatives.Where(x => x.Student_Id  == sid);
                foreach (var d in data)
                {
                    db.TE_Student_Relatives.DeleteObject(d);
                   
                }
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }


    }
}
